<?php

// Prevent much happening if this script is called directly
if ( !defined('INCS') ) exit;

/*

    Validation functions

    These functions are called from Form::validator() and provide various
    methods of input validation.

    CHANGELOG
        o 2009-04-20T22:49:00 +01:00 - Created.

*/


// Validation Error Messages

define( 'VERR_NONE',          0 );
define( 'VERR_EMPTY_FIELD',   1 );
define( 'VERR_LENGTH_INSUFF', 2 );
define( 'VERR_LENGTH_EXCEED', 3 );
define( 'VERR_BADCHARS',      4 );
define( 'VERR_EMAIL',         5 );
define( 'VERR_OPT_UNSELECT',  6 );
define( 'VERR_OPT_INVALID',   7 );
define( 'VERR_UNSPECIFIED',   9 );




require_once( INCS . 'email_validation.inc.php' );


function is_empty( $dirty )
{
    if ( trim($dirty) == '' ) return VERR_EMPTY_FIELD;
    return VERR_NONE;
}

function is_too_short( $dirty, $limit )
{
    if ( mb_strlen( $dirty, 'UTF-8' ) < $limit ) return VERR_LENGTH_INSUFF;
    return VERR_NONE;
}

function is_too_long( $dirty, $limit )
{
    if ( mb_strlen( $dirty, 'UTF-8' ) > $limit ) return VERR_LENGTH_EXCEED;
    return VERR_NONE;
}

function has_badchars( $dirty, $badchars_match )
{
    $match_result = preg_match( $badchars_match, $dirty );

    if ( $match_result === 0 ) return VERR_NONE;
    if ( !$match_result ) return VERR_UNSPECIFIED;

    return VERR_BADCHARS;
}

function is_valid_email( $dirty )
{
    if ( !is_email( $dirty ) ) return VERR_EMAIL;
    return VERR_NONE;
}

